export class DateUtil {
	public static format(date: Date | number, pattern: string): string {
		if (typeof date === 'number') {
			date = new Date(date);
		}
		const o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds(),
			'q+': Math.floor((date.getMonth() + 3) / 3),
			S: date.getMilliseconds(),
		};
		if (/(y+)/.test(pattern)) {
			pattern = pattern.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
		}
		for (const k in o) {
			if (new RegExp('(' + k + ')').test(pattern)) {
				pattern = pattern.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
			}
		}
		return pattern;
	}
	public static getCurrentDate() {
		return new Date();
	}
	public static getCurrentWeek(): Array<any> {
		const startStop = new Array();
		const currentDate = this.getCurrentDate();
		const week = currentDate.getDay();
		const month = currentDate.getDate();
		const millisecond = 1000 * 60 * 60 * 24;
		const minusDay = week !== 0 ? week - 1 : 6;
		const monday = new Date(currentDate.getTime() - minusDay * millisecond);
		const sunday = new Date(monday.getTime() + 6 * millisecond);
		startStop.push(monday); // 本周起始时间
		startStop.push(sunday); // 本周终止时间
		return startStop;
	}
	/***
	 * 获得本月的起止时间
	 */
	public static getCurrentMonth(): Array<any> {
		const startStop = new Array();
		const currentDate = this.getCurrentDate();
		let currentMonth = currentDate.getMonth();
		let currentYear = currentDate.getFullYear();
		const firstDay = new Date(currentYear, currentMonth, 1);
		if (currentMonth === 11) {
			currentYear++;
			currentMonth = 0; // 就为
		} else {
			currentMonth++;
		}
		const millisecond = 1000 * 60 * 60 * 24;
		const nextMonthDayOne = new Date(currentYear, currentMonth, 1);
		const lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
		startStop.push(firstDay);
		startStop.push(lastDay);
		return startStop;
	}
}
